global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/11_capital_purchases.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {



/* gets capital purchases used for figure A9_alm_sectors based on ALMs concordance to consitent Cernsus industries */


* import making industry flow, transpose to get capital purches
import excel ${alm_data_raw}/bea/flow1997.xls, sheet("180x123Combined") cellrange(B4:DV184) clear

* drop construction commodities and noncomparable imports
* --> aren't / cannot be matched with make table
drop if inlist(B,"233511","233512","233513","233514","233515","233516","233517")
drop if inlist(B,"233523","233524","233525","233526","233527","233528","233529")
drop if inlist(B,"233532","233533","233534","233535","233536","233543","233544")
drop if inlist(B,"233545","233551","233552","233572","233573","233574","233612")
drop if inlist(B,"233621","233624","233625","233631","233701","233703","233706")
drop if inlist(B,"233722")
drop if inlist(B,"S00300")

*drop commodity code and description
drop B C

* sum up across industries the remaining commodities
*rename variables from excel columns to industry codes
foreach var of varlist *{
    rename `var' industry`=`var'[1]'
}

*drop the industry codes
drop if _n == 1

*get into a usable format
foreach var of varlist *{
    destring `var', replace
}

*sum commodities by industry
collapse (sum) industry*

* transpose to long format
xpose, clear v
ren v1 capital 
ren _varname ind_flow_IO
replace ind_flow_IO = subinstr(ind_flow_IO, "industry", "", 1)

*merge with concordance to sic4 from 2_map_sic_mfg_sic_using.do
mmerge ind_flow_IO using ${alm_data_proc}/ind_flow_IO_sic4_concordance.dta, unmatched(both)
assert _m == 3

*break down to to SIC industry
bys sic4: gen capital_sic4 = capital * w_IO_flow_SIC

ren sic4 sic4_int
gen sic4 = string(sic4_int, "%04.0f")
keep capital_sic4 sic4
duplicates drop

*and into 6090, correct one case
mmerge sic4 using ${alm_data_proc}/cw_sic4_ind6090.dta, unmatched(both)
replace capital_sic4 = capital_sic4[1108] if sic4 == "8050"
drop if sic4 == "8053"
drop _m
gen capital_ind6090 = capital_sic4 * weight
bys ind6090: egen tot_capital_ind6090 = sum(capital_ind6090)
keep tot_capital_ind6090 ind6090
ren tot_capital_ind6090 capitalp97
duplicates drop

*labeling
label variable ind6090 "ALM consistent census industry"
label variable capitalp97 "Capital purchases 1997"

save ${final_dir}/capital_purchases_ind6090.dta, replace

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat